package com.duoec.magichour.controller.web;

import com.duoec.magichour.core.web.BaseWebController;
import com.duoec.magichour.service.CaptchaService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by ycoe on 16/11/30.
 */
@Controller
@RequestMapping("/captcha")
public class CaptchaWebController extends BaseWebController {
    private static final Logger logger = LoggerFactory.getLogger(CaptchaWebController.class);

    @Autowired
    CaptchaService captchaService;

    @RequestMapping(value = "/img", method = RequestMethod.GET)
    public void captcha(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        ServletOutputStream out;
        try {
            out = response.getOutputStream();
            captchaService.captcha(out);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
}
